import random
import queue
class Process:
    def __init__(self, pid, time):
        self.pid = pid
        self.run_time = time
        self.waiting = 0
        self.start_time = 0
        self.end_time = 0
        self.now_time = time
        self.first = True
class FCFS:
    def __init__(self):
        self.ready_queue = queue.Queue()
        self.current_time = 0
        self.completed = []

    def add_process(self, process):
        self.ready_queue.put(process)

    def run(self):
        print("调度开始")
        timesize = 5#时间片
        while not self.ready_queue.empty():
            process = self.ready_queue.get()
            if process.now_time != 0:
                if process.first:
                    process.start_time = self.current_time
                    process.first = False
                if process.now_time >= timesize:
                    self.current_time += timesize
                    process.now_time -= timesize
                else:
                    self.current_time += process.now_time
                    process.now_time = 0
                self.ready_queue.put(process)
            else:
                process.end_time = self.current_time
                self.completed.append(process)
    def print_process(self):
        print("PID\t服务时间\t完成时间\t周转时间\t带权周转时间")
        turntime = 0
        for p in self.completed:
            print(f"{p.pid}\t\t{p.run_time}\t\t{p.end_time}\t\t{p.end_time}\t\t{p.end_time/p.run_time}")
            turntime += p.end_time
        print(f"平均周转时间{turntime / len(self.completed)}")
if __name__ == "__main__":
    fcfs =FCFS()
    process = []
    for pid in range(1,random.randint(1,100)):
        time = random.randint(10,100)
        process.append(Process(pid,time))

    for p in process:
        fcfs.add_process(process=p)
    fcfs.run()
    fcfs.print_process()